package com.cyou.qb.common.service.impl;

import com.cyou.qb.service.BaseService;
import com.cyou.qb.system.AdminUserContext;
import com.cyou.qb.system.ServiceContext;
import com.cyou.qb.system.UserContext;

public abstract class BaseSerivceImpl implements BaseService{
	
	
	private ThreadLocal<ServiceContext> threadLocal = new ThreadLocal<ServiceContext>() {
		@Override
		protected synchronized ServiceContext initialValue() {
			return new ServiceContext();
		}
	};

	public ServiceContext getServiceContext() {
		return threadLocal.get();
	}

	public void setServiceContext(ServiceContext serviceContext) {
		threadLocal.set(serviceContext);
	}
	
	public void freeServiceContextContext() {
		threadLocal.remove();
	}

	/**
	 * 当前登录用户上下文
	 * @return
	 */
	protected UserContext getUserContext(){
		return getServiceContext().getUserContext();
	}
	
	/**
	 * 获取当前管理员角色
	 * @return
	 */
	protected int getUserRole(){
		return Integer.parseInt(((AdminUserContext)getUserContext()).getType());
	}
}
